package com.xyf.statistics.service.impl;

import com.seventh.common.pojo.R;
import com.xyf.statistics.mapper.LogStatisticsMapper;
import com.xyf.statistics.pojo.entity.SysOPerLog;
import com.xyf.statistics.service.LogStatisticsService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class LogStatisticsServiceImpl implements LogStatisticsService {

    @Resource
    LogStatisticsMapper mapper;
    @Override
    public R selectLog(String ymonth) {

//        String dateString=ymonth;
//        SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd yyyy HH:mm:ss 'GMT'Z", Locale.ENGLISH);
//        Date dd = null; //将字符串改为date的格式
//        try {
//            dd = sdf.parse(dateString);
//        } catch (ParseException e) {
//            e.printStackTrace();
//        }
//        String resDate= new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(dd);
//        String newYear=resDate.substring(0,4);

        System.out.println(ymonth);

        List<SysOPerLog> list= mapper.selectLog(ymonth);
        System.out.println("list=="+list);

        List  hedate=new ArrayList();//所有日期转换为前台的data数组
        List  count1=new ArrayList();//操作总次数转换为前台的count数组
        List  count2=new ArrayList();//操作总次数转换为前台的count数组
        List  count3=new ArrayList();//操作总次数转换为前台的count数组
        for (SysOPerLog log:list) {
            hedate.add(String.valueOf(log.getYearMonth()));
        }
        //循环遍历分别拿出addOps和updateOps以及deleteOps
        for (SysOPerLog log:list) {
            count1.add(Integer.valueOf(log.getAddOps()));
            count2.add(Integer.valueOf(log.getUpdateOps()));
            count3.add(Integer.valueOf(log.getDeleteOps()));
        }

//将{onedata：data1，onecount：count1,twodata:data2,twocount:count2,threedata:data3,threecount:count3}
        Map countdata=new HashMap<>();
        countdata.put("hedate",hedate);
        countdata.put("onecount",count1);
        countdata.put("twocount",count2);
        countdata.put("threecount",count3);
        return R.ok().put("countdata",countdata);
    }
}
